package top.tocchen.bookshop.controller;

import top.tocchen.bookshop.entity.BookEntity;
import top.tocchen.bookshop.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
/**
 * @author tocchen
 * @date 2022/8/9 19:34
 * @since jdk 1.8
 **/
@Controller
public class SearchController {
    @Autowired
    SearchService searchService;

    @RequestMapping("/search")
    public String goSearch(){
        return "search";
    }

    @RequestMapping("/search/int")
    public String search(ModelMap map, @RequestParam("keyword") Integer categoryid){
        List<BookEntity> books = searchService.findByCategoryId(categoryid);
        map.addAttribute("results",books);
        return "search";
    }

    @RequestMapping("/search/str")
    public String search(ModelMap map,@RequestParam("findstr") String str){
        List<BookEntity> books = searchService.findByStr("%"+str+"%");
        map.addAttribute("results",books);
        map.addAttribute("str",str);
        return "search";
    }
}
